<template>
  <div class="lover-container">
    <van-nav-bar title="秀恩爱" fixed>
      <template #left>
        <span @click="$router.back()">
          <svg-icon style="margin-left: 10px;" icon-class='fanhui' width="30px" height="30px" class="icon"/>
        </span>
      </template>
    </van-nav-bar>
    <div class="content">
      <!-- 中间 聊天内容区域 -->
      <div class="main">
        <div class="dynamicInfo" v-for="(item) in dynamicDataList" :key="item.mic_id">
          <div class="imgWrap">
            <van-image
              round
              width="1rem"
              height="1rem"
              fit="cover"
              :src="item.aut_pic"
            />
          </div>
          <div class="userContent">
            <div class="uNameWrap">
              <div class="uName">{{ item.aut_name }}</div>
              <span class="deleteBtn" @click="deleteFn(item.mic_id)" v-if="item.aut_id===userInfo.id">
                <svg-icon style="margin-right: 10px;" icon-class='shanchu' width="20px" height="20px" class="icon"/>
              </span>
            </div>
            <div class="contentText">{{ item.content }}</div>
            <van-image
              class="coverImg"
              width="2.5rem"
              height="2.5rem"
              fit="cover"
              v-for="(el,index) in item.cover.split('$$')" :key="index"
              :src="el"
              @click="previewImages(el,index)"
            />
            <div class="location">
              <svg-icon icon-class='xin-dingwei' width="15px" height="15px" class="icon"/>
              <div>{{ item.IpLocation }}</div>
            </div>
            <div class="contentFooter">
              <div class="pubDate">{{ item.pubDate | relativeTime }}</div>
              <div class="iconRight">
                <van-badge :content="item.countNum?item.countNum:0" color="#fff">
                  <span @click="getMicId(item.mic_id)">
                  <svg-icon style="margin-right: 12px;" icon-class='liuyan' width="20px" height="20px" class="icon"/>
                </span>
                </van-badge>
                <like-article :attitude.sync="item.attitude" :articleId="item.mic_id"></like-article>
              </div>
            </div>

          </div>
        </div>
      </div>

      <!-- 评论列表 -->
      <van-popup v-model="isCommentShow" round position="bottom" :style="{ height: '48%' }" @closed="getMsg">
        <comment-list
          ref="comment-list"
          :sourceId="clickCommentId"
          @onloadSuccess="onloadSuccessFn"
          @replyClick="replyClickFn"
          v-if="isCommentShow"
        ></comment-list>
        <!-- 写评论按钮 -->
        <van-button
          class="comment-btn"
          type="default"
          round
          size="small"
          @click="isPostShow = true"
        >写评论
        </van-button>
      </van-popup>

      <!-- 弹框部分，写在整个模板的最下面 -->
      <!-- 评论提交弹框 -->
      <van-popup v-model="isPostShow" position="bottom">
        <comment-post
          :idType="idType"
          :targetId="clickCommentId"
          :artId="clickCommentId"
          @postSuccess="postSuccessFn"
        ></comment-post>
      </van-popup>

      <!-- 评论回复弹框 -->
      <van-popup v-model="isReplyShow" position="bottom" style="height: 100%;">
        <comment-reply
          v-if="isReplyShow"
          :artId="clickCommentId"
          :comment="currentReplyComment"
          @closeReply="isReplyShow = false"
        ></comment-reply>
      </van-popup>

    </div>

    <!-- 添加新动态按钮 -->
    <div class="drift" @click="$router.push({ path: '/dynamic/add' })">
      <svg-icon icon-class='xianxingyumaobi' width="30px" height="30px" class="icon"/>
    </div>
  </div>
</template>

<script>
import { deleteMicAPI, getMicAPI, getUserInfoAPI } from '@/api'
import { Dialog, ImagePreview } from 'vant'
import CommentList from '@/views/article/components/comment-list.vue'
import CommentPost from '@/views/article/components/comment-post.vue'
import CommentReply from '@/views/article/components/comment-reply.vue'
import LikeArticle from '@/views/article/components/like-article.vue'
import dayjs from 'dayjs'

export default {
  name: 'dynamicIndex',
  components: {
    CommentList,
    CommentPost,
    CommentReply,
    LikeArticle
  },
  data () {
    return {
      userInfo: {},
      loverInfo: [],
      dynamicDataList: [],
      cont: '',
      isCommentShow: false,
      clickCommentId: 0,
      isLoading: false, // 加载状态
      errorStatus: 200, // 失败时的状态码
      // articleInfo: {}, // 文章详情信息
      totalCommentCount: 0, // 文章评论总数量
      isPostShow: false, // 写评论弹框状态
      isReplyShow: false, // 评论回复弹框状态
      currentReplyComment: {}, // 评论回复弹框中的目标文章评论内容
      idType: 'a'
    }
  },
  created () {
    this.loadUserInfo()
    this.getMsg()
  },

  methods: {
    // 请求用户信息
    async loadUserInfo () {
      try {
        const res = await getUserInfoAPI()
        console.log(res)
        this.userInfo = res.data.data
        this.loverInfo = res.data.loverData
      } catch (error) {
        console.log(error)
      }
    },

    // 接收动态
    async getMsg () {
      try {
        const res = await getMicAPI({ status: true })
        console.log(res)
        if (res.data.status === 0) {
          this.dynamicDataList = res.data.message.sort(function (a, b) {
            return dayjs(a.pubDate).isBefore(dayjs(b.pubDate)) ? 1 : -1
          })
          console.log(this.dynamicDataList)
        }
      } catch (err) {
        console.log(err)
      }
    },
    // 图片放大预览
    previewImages (url, index) {
      ImagePreview({
        images: [url],
        showIndex: false
      })
    },

    // 点击评论
    getMicId (sourceId) {
      this.clickCommentId = sourceId
      this.isCommentShow = true
    },
    // 文章列表更新成功后触发的方法
    onloadSuccessFn (total) {
      // 记录评论总数
      this.totalCommentCount = total
    },

    // 触发评论回复弹框内容
    replyClickFn (currentComment) {
      // 将子组件传递过来的评论对象赋值给 currentReplyComment
      this.currentReplyComment = currentComment
      console.log('currentComment', currentComment)
      // 开启评论回复弹框
      this.isReplyShow = true
    },

    // 发布评论成功后触发的方法
    postSuccessFn (newComment) {
      this.isPostShow = false
      this.$refs['comment-list'].addComment(newComment)
    },
    async deleteFn (micId) {
      Dialog.confirm({
        title: '确定删除吗？'
      })
        .then(async () => {
          // on confirm
          await deleteMicAPI(micId)
          this.$toast.success('删除成功！')
          await this.getMsg()
          this.isAddShow = false
        })
        .catch(() => {
          // on cancel
        })
    }
  }
}
</script>

<style scoped lang="less">
.lover-container {
  background-color: #fff;

  .van-nav-bar {
    &::after {
      border: none;
    }
  }

  .content {
    padding-top: 50px;

    .main {
      padding: 20px 30px 20px 20px;

      > div:last-child {
        border: none;
      }

      .dynamicInfo {
        display: flex;
        border-bottom: 1px solid #ccc;
        padding-bottom: 15px;
        margin-bottom: 15px;

        .imgWrap {
          margin-right: 13px;

          .van-image {
            border: 2px solid #ec87c0;
          }
        }

        .userContent {
          font-size: 14px;

          .uNameWrap {
            font-size: 16px;
            color: #919191;
            line-height: 37px;
            display: flex;
            align-items: center;
            justify-content: space-between;
          }

          .contentText {
            padding-bottom: 8px;
          }

          .coverImg {
            margin-right: 3px;
            margin-bottom: 10px;
          }

          .location {
            color: #b3b3b3;
            display: flex;
            font-size: 12px;
            line-height: 25px;
            align-items: center;
            justify-content: start;
          }

          .contentFooter {
            width: 270px;
            color: #b3b3b3;
            font-size: 12px;
            display: flex;
            align-items: center;
            justify-content: space-between;

            .iconRight {
              display: flex;
              align-items: center;
            }
          }
        }

      }
    }

    .comment-btn {
      position: absolute;
      bottom: 8px;
      z-index: 1;
      left: 50%;
      transform: translateX(-50%);
      width: 50%;
      background-color: #ac92eb;
      color: #fff;
    }
  }

  .drift {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 45px;
    height: 45px;
    background-color: #f1a760;
    border-radius: 50%;
    position: fixed;
    bottom: 13%;
    right: 10%;
    box-shadow: 3px 2px 2px #ccc;
    z-index: 100;
  }

  ::v-deep .van-badge {
    right: 8px;
    font-size: 10px;
    color: #ec87c0;
  }
}
</style>
